package com.ic.service.impl;

import com.ic.base.AbstractBaseService;
import com.ic.base.PageInfo;
import com.ic.dao.ItemUseDao;
import com.ic.entity.ItemUse;
import com.ic.service.ItemUseService;
import com.ic.util.KeyGenerateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 物品使用业务类
 * Author: spark
 * Date  : 2017/6/25
 * Time  : 17:29.
 */
@Service("itemUseService")
public class ItemUseServiceImpl extends AbstractBaseService<ItemUse> implements ItemUseService {
	@Resource
	private ItemUseDao itemUseDao;

	@Override
	public Integer insert(ItemUse itemUse) {
		itemUse.setId(KeyGenerateUtil.getInstance().getKey());
		return itemUseDao.insert(itemUse);
	}

	@Override
	public Integer updateByPrimaryKey(ItemUse itemUse) {
		return itemUseDao.updateByPrimaryKey(itemUse);
	}

	@Override
	public Integer deleteByPrimaryKey(Integer id) {
		return itemUseDao.deleteByPrimaryKey(id);
	}

	/**
	 * 根据查询条件获取领用信息
	 *
	 * @param itemUse  领用信息
	 * @param pageInfo 分页信息
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showItemUseList(ItemUse itemUse, PageInfo pageInfo) {
		Map queryMap = new HashMap();
		Map resultMap = new HashMap();
		List list = new ArrayList();
		queryMap.put("subjectName", itemUse.getSubjectName());
		queryMap.put("itemName", itemUse.getItemName());
		queryMap.put("itemInfoNum", itemUse.getItemInfoNum());
		queryMap.put("receiptorId", itemUse.getReceiptorId());
		queryMap.put("state", itemUse.getUseState());
		// 查询符合条件的记录总数
		int totalSize = itemUseDao.selectCount(queryMap);
		if (totalSize == 0) {
			resultMap.put("rows", 0);
			resultMap.put("total", list);
			return resultMap;
		}
		pageInfo.setTotalSize(totalSize);
		queryMap.put("startIndex", pageInfo.getStartIndex());
		queryMap.put("pageSize", pageInfo.getPAGE_SIZE());
		// 查询符合条件的记录信息
		list = itemUseDao.showItemUseList(queryMap);
		resultMap.put("rows", list);
		resultMap.put("total", totalSize);
		return resultMap;
	}

	/**
	 * 根据领用信息id查看领用信息
	 *
	 * @param itemUseId 领用信息id
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showItemUse(String itemUseId) {
		Map map = new HashMap<>();
		ItemUse itemUse = (ItemUse) itemUseDao.selectItemUseById(itemUseId).get(0);
		map.put("itemUse", itemUse);
		return map;
	}
}

